
############################################################################
#基期设定
# date_base<-24133  #基期设定为2011年1月
# city_code<-'qd'   #城市设定为青岛
# pty<-11           #产品类型
############################################################################

#抽取数据
source('R/load_spatial_data.R')

#ha_price与ha_phase数据处理
library(dplyr)
price_sale<-subset(tabln.vec$ha_price,proptype==pty)[,c('ymid','ha_code','saleprice','salebldgarea')]%>%na.omit
# phase<-tabln.vec$ha_phase%>%
#     filter(!is.na(buildyear))%>%
#     group_by(ha_code)%>%
#     summarise(buildyear=round(mean(buildyear)))%>%as.data.frame

# #关联phase数据--建筑年代
# pp_sale<-merge(price_sale,phase,by='ha_code')

#关联POI与info数据
ppi_sale<-merge(price_sale,ha_info.sp,by='ha_code')

data_base<-subset(ppi_sale,ymid>=date_base_begin&ymid<=date_base_end&city_code==city_code)
myvar_city<-names(data_base) %in% c('ymid','city_code','ha_code','proptype','salecount','name','ha_cl_code','ha_cl_name','dist_code','dist_name','x','y','volume_rate','greening_rate')
data_base1<-data_base[!myvar_city]

if(nrow(data_base1)>=30){data_base2<-data_base1[1,]}else{stop("该基期样本量不足30，需重新设定")}

len_base<-length(names(data_base2))
for(ii in 1:len_base){
    name_base<-names(data_base2)[ii]
    data_base2[,name_base]<-mean(data_base1[,name_base],na.rm=T)
}

#基点为100的估价计算
data_base3<-subset(ppi_sale,ymid==date_base&city_code==city_code)
data_base4<-data_base3[!myvar_city]

fit_base<-lm(log(saleprice)~.,data = data_base4)
price.pre_base<-predict(fit_base,newdata=data_base2)[[1]]%>%exp

##data_base2为基期的标准建筑各特征值
##price.pre_base为该城市在基期的预测价格
